package com.scanner.hardware;


import android.content.SharedPreferences;
import android.os.Bundle;
import android.app.Fragment;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.widget.CompoundButton;
import android.widget.RadioGroup;

import com.scanner.barcodeservice.SerialPortService;

/**
 * A simple {@link Fragment} subclass.
 */
public class SettingFragment extends PreferenceFragment {

    SerialPortService.MyBinder  mBinder = null;
    public SettingFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference_settings);
        //getPreferenceManager().setSharedPreferencesName("config");
        addPreferenceHead(getPreferenceScreen());
    }
    public void addPreferenceHead(PreferenceScreen mPreferenceScreen){

        PreferenceHead ph = new PreferenceHead(getActivity());
        ph.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                int radioButtonId = group.getCheckedRadioButtonId();

                SharedPreferences mSP = getActivity().getSharedPreferences(MyApplication.SYMBOL_CONFIG_SIG, 0);
                SharedPreferences.Editor editor = mSP.edit();

                if( radioButtonId == R.id.radioSig){
                    if(mBinder != null){
                        mBinder.openUartSig();
                    }

                    editor.putBoolean(MyApplication.SCAN_MODE_SIG,  true);

                }else if( radioButtonId == R.id.radioQR){

                    if(mBinder != null){
                        mBinder.openUartQR();
                    }

                    editor.putBoolean(MyApplication.SCAN_MODE_SIG,  false);

                }

                editor.commit();
            }
        });

        ph.setOnCheckedChangeScankey(new android.widget.CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                int ButtonId = buttonView.getId();
                SharedPreferences mSP = getActivity().getSharedPreferences(MyApplication.SYMBOL_CONFIG_SIG, 0);
                SharedPreferences.Editor editor = mSP.edit();
                if(ButtonId == R.id.radioLeft){
                    if(isChecked){
                        editor.putBoolean(MyApplication.SCAN_MODE_LEFT,  true);
                    }else{
                        editor.putBoolean(MyApplication.SCAN_MODE_LEFT,  false);
                    }

                }else if(ButtonId == R.id.radioRight){

                    if(isChecked){
                        editor.putBoolean(MyApplication.SCAN_MODE_RIGHT,  true);
                    }else{
                        editor.putBoolean(MyApplication.SCAN_MODE_RIGHT,  false);
                    }
                }
                editor.commit();
            }

        });
        ph.setOrder(-1);
        mPreferenceScreen.addPreference(ph);
    }
}
